﻿using System;
using System.Collections.Generic;
using System.Linq;
using Framework.Exceptions.RepositoryExceptions;
using Interfaces.Repositories;
using Interfaces.RepositoryModels.InternalModels.NewsInformationModels;

namespace SQLRepositories.NewsRepositories
{
    public class NewsDataItemRepository : EntityBaseRepository<NewsDataItemModel>, INewsDataItemRepository
    {
        public NewsDataItemRepository(IDBEconomicInformationServiceContext context) : base(context)
        {}

        public void Create(NewsDataItemModel item)
        {
            Context.News.Add(item);
            Context.SaveChanges();
        }

        public NewsDataItemModel GetById(Guid id)
        {
            var result = from item in Context.News where item.Id.Equals(id) select item;
            return result.FirstOrDefault();
        }

        public void Delete(Guid id)
        {
            var removedItem = (from item in Context.News 
                               where item.Id.Equals(id) select item).FirstOrDefault();
            if(removedItem == null)
                throw new EntityNotFoundException("News");
            Context.News.Remove(removedItem);
            Context.SaveChanges();
        }

        public void Delete(NewsDataItemModel item)
        {
            Context.News.Remove(item);
            Context.SaveChanges();
        }

        public IEnumerable<NewsDataItemModel> GetAll()
        {
            return Context.News;
        }
    }
}
